library(ggplot2)
library(dplyr)
library(readr)
library(scales)
library(tidyr)
library(patchwork)

df <- read_csv("acled_country_final.csv")

# Variance of fatalities_mean by country
country_var <- df %>%
  group_by(country) %>%
  summarise(var_fatalities = var(fatalities_mean, na.rm = TRUE),
            n_events = n()) %>%
  arrange(desc(var_fatalities))

print(head(country_var, 20), 20)

selected_country <- "Togo"

df_plot <- df %>%
  filter(country == selected_country) %>%
  arrange(event_date) %>%
  mutate(event_date = as.Date(event_date)) %>%
  mutate(cumulated_fatal_weighted_log = log1p(cumulated_fatal_weighted))

# -------------------------------
p_top <- ggplot(df_plot, aes(x = event_date)) +
  geom_line(aes(y = fatalities_mean, color = "Daily Fatalities"), size = 1.0) +
  geom_line(aes(y = cumulated_fatal_weighted, color = "Reference Point"), size = 1.0) +
  labs(
    x = NULL,
    y = "Fatalities (logged)",
    color = NULL
  ) +
  theme_minimal() +
  scale_color_manual(values = c("Daily Fatalities" = "steelblue",
                                "Reference Point" = "firebrick")) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
  theme(legend.position = "top", legend.justification = "left",
        text = element_text(size = 14),
        plot.title = element_text(face = "bold", size = 16, hjust = 0.5),
        axis.title.x = element_text(size = 15, margin = margin(t = 15)),
        axis.title.y = element_text(size = 15, margin = margin(r = 15)),
        axis.text = element_text(size = 15),
        legend.text = element_text(size = 15))

# -------------------------------
p_bottom <- ggplot(df_plot, aes(x = event_date)) +
  geom_line(aes(y = fatality_diff_signed_weighted, color = "Discrepancy"), size = 1.0) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray40") +
  labs(
    x = "Event Date",
    y = "Discrepancy (logged)",
    color = NULL
  ) +
  theme_minimal() +
  scale_color_manual(values = c("Discrepancy" = "darkgreen")) +
  theme(legend.position = "top", legend.justification = "left",
        text = element_text(size = 14),
        plot.title = element_text(face = "bold", size = 16, hjust = 0.5),
        axis.title.x = element_text(size = 15, margin = margin(t = 15)),
        axis.title.y = element_text(size = 15, margin = margin(r = 15)),
        axis.text = element_text(size = 15),
        legend.text = element_text(size = 15))

# -------------------------------
combined_plot <- p_top / p_bottom + plot_layout(heights = c(2, 1))

print(combined_plot)

################################################################################
df_plot_exp <- df %>%
  filter(country == selected_country) %>%
  arrange(event_date) %>%
  mutate(event_date = as.Date(event_date)) %>%
  mutate(cumulated_fatal_weighted_exp_log = log1p(cumulated_fatal_weighted_exp))

# -------------------------------
p_top_exp <- ggplot(df_plot_exp, aes(x = event_date)) +
  geom_line(aes(y = fatalities_mean, color = "Daily Fatalities"), size = 1.0) +
  geom_line(aes(y = cumulated_fatal_weighted_exp, color = "Reference Point (Exp)"), size = 1.0) +
  labs(
    x = NULL,
    y = "Fatalities (logged)",
    color = NULL
  ) +
  theme_minimal() +
  scale_color_manual(values = c("Daily Fatalities" = "steelblue",
                                "Reference Point (Exp)" = "firebrick")) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
  theme(legend.position = "top", legend.justification = "left",
        text = element_text(size = 14),
        plot.title = element_text(face = "bold", size = 16, hjust = 0.5),
        axis.title.x = element_text(size = 15, margin = margin(t = 15)),
        axis.title.y = element_text(size = 15, margin = margin(r = 15)),
        axis.text = element_text(size = 15),
        legend.text = element_text(size = 15))

# -------------------------------
p_bottom_exp <- ggplot(df_plot_exp, aes(x = event_date)) +
  geom_line(aes(y = fatality_diff_signed_weighted_exp, color = "Discrepancy (Exp)"), size = 1.0) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray40") +
  labs(
    x = "Event Date",
    y = "Discrepancy (logged, Exp)",
    color = NULL
  ) +
  theme_minimal() +
  scale_color_manual(values = c("Discrepancy (Exp)" = "darkgreen")) +
  theme(legend.position = "top", legend.justification = "left",
        text = element_text(size = 14),
        plot.title = element_text(face = "bold", size = 16, hjust = 0.5),
        axis.title.x = element_text(size = 15, margin = margin(t = 15)),
        axis.title.y = element_text(size = 15, margin = margin(r = 15)),
        axis.text = element_text(size = 15),
        legend.text = element_text(size = 15))

# -------------------------------
combined_plot_exp <- p_top_exp / p_bottom_exp + plot_layout(heights = c(2, 1))

print(combined_plot_exp)
